﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace HoaDon
{
	public partial class DM_Mon : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				LoadDropDownList();
				BindData();
				lbError.Text = "";
			}
		}

		private void BindData()
		{
			if (drNhomMon.Items.Count <= 0)
				return;
			string nhvt2id = drNhomMon.SelectedValue.ToString();
			DataTable dtNhvt2 = new DataTable();
			dtNhvt2 = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT TOP 1 NL_yn, TP_yn FROM dmnhvt2 WHERE Nhvt2ID = '" + nhvt2id + "' AND FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");

			Session["DMMon_NL_yn"] = dtNhvt2.Rows[0]["NL_yn"].ToString();
			Session["DMMon_TP_yn"] = dtNhvt2.Rows[0]["TP_yn"].ToString();

			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmvt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_Nhvt2ID = '" + nhvt2id + "' AND Status = '1'");

			gdvban.DataSource = dt;
			gdvban.DataBind();
			if (dt.Rows.Count == 0)
			{
				DataRow dtr = dt.NewRow();
				dtr["NL_yn"] = dtNhvt2.Rows[0]["NL_yn"];
				dtr["TP_yn"] = dtNhvt2.Rows[0]["TP_yn"];
				dt.Rows.Add(dtr);
				gdvban.DataSource = dt;
				gdvban.DataBind();
				gdvban.Rows[0].Enabled = false;
			}
		}

		protected DataTable getDataSource()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmdvt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");
			return dt;
		}

		private void LoadDropDownList()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmnhvt2 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");

			drNhomMon.DataSource = dt;
			drNhomMon.DataValueField = "nhvt2ID";
			drNhomMon.DataTextField = "nhvt2Name";
			drNhomMon.DataBind();
			if (drNhomMon.Items.Count > 0)
				drNhomMon.SelectedIndex = 0;
		}

		protected void EditDM(object sender, GridViewEditEventArgs e)
		{
			gdvban.EditIndex = e.NewEditIndex;
			BindData();
			lbError.Text = "";
		}
		protected void CancelEditDM(object sender, GridViewCancelEditEventArgs e)
		{
			gdvban.EditIndex = -1;
			BindData();
			lbError.Text = "";
		}

		protected void OnPaging(object sender, GridViewPageEventArgs e)
		{
			BindData();
			gdvban.PageIndex = e.NewPageIndex;
			gdvban.DataBind();
			lbError.Text = "";
		}

		protected void UpdateDM(object sender, GridViewUpdateEventArgs e)
		{
			string id = ((Label)gdvban.Rows[e.RowIndex].FindControl("vtID")).Text;
			string name = ((TextBox)gdvban.Rows[e.RowIndex].FindControl("tbvtName")).Text;
			string dvt = ((DropDownList)gdvban.Rows[e.RowIndex].FindControl("drdvt")).SelectedValue.ToString();
			string giaban = ((TextBox)gdvban.Rows[e.RowIndex].FindControl("tbGia_Ban0")).Text;
			giaban = giaban.Replace(",", string.Empty);
			giaban = giaban.Replace(".", string.Empty);
			string giamua = ((TextBox)gdvban.Rows[e.RowIndex].FindControl("tbGia_Mua0")).Text;
			giamua = giamua.Replace(",", string.Empty);
			giamua = giamua.Replace(".", string.Empty);
			if (id == "")
			{
				lbError.Text = "Không để trống mã";
				return;
			}
			if (name == "")
			{
				lbError.Text = "Không để trống tên";
				return;
			}
			if (giaban == "")
			{
				lbError.Text = "Không để trống giá bán";
				return;
			}
			if (giamua == "")
			{
				lbError.Text = "Không để trống giá mua";
				return;
			}

			try
			{
				string sCommand = "UPDATE dmvt SET ";
				sCommand = sCommand + "vtName = N'" + name + "', ";
				sCommand = sCommand + "User_id2='" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "', ";
				sCommand = sCommand + "datetime2 = GETDATE(), ";
				sCommand = sCommand + "FK_DvtID = N'" + dvt + "', ";
				sCommand = sCommand + "Gia_mua0 = " + giamua + ", ";
				sCommand = sCommand + "Ck_Mua = " + "0" + ", ";
				sCommand = sCommand + "Gia_mua = " + giamua + ", ";
				sCommand = sCommand + "Gia_Ban0 = " + giaban + ", ";
				sCommand = sCommand + "Ck_Ban11 = " + "0" + ", ";
				sCommand = sCommand + "Gia_Ban11 = " + giaban + ", ";
				sCommand = sCommand + "Ck_Ban12 = " + "0" + ", ";
				sCommand = sCommand + "Gia_Ban12 = " + giaban + ", ";
				sCommand = sCommand + "Ck_Ban13 = " + "0" + ", ";
				sCommand = sCommand + "Gia_Ban13 = " + giaban + ", ";
				sCommand = sCommand + "Ck_Ban21 = " + "0" + ", ";
				sCommand = sCommand + "Gia_Ban21 = " + giaban + ", ";
				sCommand = sCommand + "Ck_Ban22 = " + "0" + ", ";
				sCommand = sCommand + "Gia_Ban22 = " + giaban + ", ";
				sCommand = sCommand + "Ck_Ban23 = " + "0" + ", ";
				sCommand = sCommand + "Gia_Ban23 = " + giaban + ", ";
				sCommand = sCommand + "SlMin = " + "0" + ", ";
				sCommand = sCommand + "SlMax = " + "0" + ", ";
				sCommand = sCommand + "SlLow = " + "0" + ", ";
				sCommand = sCommand + "NL_yn = " + Session["DMMon_NL_yn"].ToString() + ", ";
				sCommand = sCommand + "TP_yn = " + Session["DMMon_TP_yn"].ToString() + " ";
				sCommand = sCommand + "WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND vtID= '" + id + "'";

				SqlEXEC.ExecCommand(Session["strConn"].ToString(), sCommand);
				gdvban.EditIndex = -1;
				BindData();
				lbError.Text = "";
			}
			catch (Exception ex)
			{
				lbError.Text = "Xảy ra lỗi khi cập nhật dữ liệu";
				gdvban.EditIndex = -1;
				BindData();
			}
		}

		protected void DeleteDM(object sender, EventArgs e)
		{
			Button bt = (Button)sender;
			try
			{
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "DELETE FROM dmvt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND vtID= '" + bt.CommandArgument + "'");
			}
			catch (Exception ex)
			{
				lbError.Text = "Đã phát sinh dữ liệu không thể xóa";
			}
			BindData();
		}

		protected void AddNewDM(object sender, EventArgs e)
		{
			if (drNhomMon.Items.Count <= 0)
				return;
			try
			{
				string Nhvvid = drNhomMon.SelectedValue.ToString();
				string id = ((TextBox)gdvban.FooterRow.FindControl("txtvtID")).Text;
				string name = ((TextBox)gdvban.FooterRow.FindControl("txtvtName")).Text;
				string dvt = ((DropDownList)gdvban.FooterRow.FindControl("drfdvt")).SelectedValue.ToString();
				string giaban = ((TextBox)gdvban.FooterRow.FindControl("txtGia_Ban0")).Text;
				giaban = giaban.Replace(",", string.Empty);
				giaban = giaban.Replace(".", string.Empty);
				string giamua = ((TextBox)gdvban.FooterRow.FindControl("txtGia_Mua0")).Text;
				giamua = giamua.Replace(",", string.Empty);
				giamua = giamua.Replace(".", string.Empty);

				if (id == "")
				{
					lbError.Text = "Không để trống mã";
					return;
				}
				if (name == "")
				{
					lbError.Text = "Không để trống tên";
					return;
				}
				if (giaban == "")
				{
					lbError.Text = "Không để trống giá bán";
					return;
				}
				if (giamua == "")
				{
					lbError.Text = "Không để trống giá mua";
					return;
				}
				string sCommand = "INSERT INTO dmvt (VtID,FK_DatabaseID,FK_VtID,Code,VtName,FK_Nhvt2ID,FK_DvtID,FK_TkID_vt,FK_TkID_gv,FK_TkID_dt,FK_TkID_tl,MaThue,MaThueNK,Vat,Gia_mua0,Ck_Mua,Gia_mua,Gia_Ban0,Ck_Ban11,Gia_ban11,Ck_Ban12,Gia_Ban12,Ck_Ban13,Gia_Ban13,Ck_Ban21,Gia_Ban21,Ck_Ban22,Gia_Ban22,Ck_ban23,Gia_Ban23,SlMin,SlMax,SlLow,DienGiai,Status,datetime0,datetime2,user_id0,user_id2,NL_yn,TP_yn) VALUES (";
				sCommand = sCommand + "'" + id + "','";
				sCommand = sCommand + Session["FK_DatabaseID"].ToString() + "','";
				sCommand = sCommand + "" + "','";
				sCommand = sCommand + "" + "',N'";
				sCommand = sCommand + name + "','";
				sCommand = sCommand + Nhvvid + "',N'";
				sCommand = sCommand + dvt + "','";
				sCommand = sCommand + "156" + "','";
				sCommand = sCommand + "632" + "','";
				sCommand = sCommand + "5111" + "','";
				sCommand = sCommand + "5212" + "','";
				sCommand = sCommand + "" + "','";
				sCommand = sCommand + "" + "',";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giamua + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giamua + ",";
				sCommand = sCommand + giaban + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giaban + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giaban + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giaban + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giaban + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giaban + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + giaban + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + "0" + ",";
				sCommand = sCommand + "0" + ",N'";
				sCommand = sCommand + "" + "','";
				sCommand = sCommand + "1" + "',";
				sCommand = sCommand + "GETDATE()" + ",";
				sCommand = sCommand + "GETDATE()" + ",'";
				sCommand = sCommand + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
				sCommand = sCommand + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "',";
				sCommand = sCommand + Session["DMMon_NL_yn"].ToString() + ",";
				sCommand = sCommand + Session["DMMon_TP_yn"].ToString() + ")";
				if (SqlEXEC.ExecCommand(Session["strConn"].ToString(), sCommand) == -1)
					lbError.Text = "Mã đã tồn tại, chọn mã khác";
				else
				{
					BindData();
					lbError.Text = "";
				}
			}
			catch (Exception ex)
			{
				lbError.Text = "Phải chọn nhóm món!";
				return;
			}

		}

		protected void ViewTPDM(object sender, GridViewSelectEventArgs e)
		{
			string id = ((Label)gdvban.Rows[e.NewSelectedIndex].FindControl("vtID")).Text;
			string name = ((Label)gdvban.Rows[e.NewSelectedIndex].FindControl("vtName")).Text;
			string nl = ((CheckBox)gdvban.Rows[e.NewSelectedIndex].FindControl("NL")).Checked ? "1" : "0";
			if (Session["DMMon_NL_yn"].ToString() == "0")
			{
				Session["Mon"] = id;
				Session["TenMon"] = name;
				Response.Redirect("~\\DM_TPham.aspx");
			}
			else
			{
				e.Cancel = true;
				BindData();
				lbError.Text = "";
			}
		}

		protected void drNhomMon_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindData();
			lbError.Text = "";
		}

	}
}
